package com.activenetwork.custom.webelement.decorate;

import java.lang.reflect.Field;

import org.openqa.selenium.SearchContext;
import org.openqa.selenium.support.pagefactory.ElementLocator;
import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.activenetwork.util.EnvContext;

public class WebElementLocatorFactory implements ElementLocatorFactory{
	  private static Logger logger = LoggerFactory.getLogger(WebElementLocatorFactory.class); 
	 
	 /**
	 * Used by spring injection
	 * pay attention at time of injection, thread webdriver instance is NOT created
	 * so here we get NULL from thread local
	 */
	  public WebElementLocatorFactory(){}

	  public ElementLocator createLocator(Field field) {
		SearchContext searchContext = EnvContext.webDriverTL.get();
	    try{
	    	return new WebElementLocator(searchContext, field);
	    }catch(IllegalArgumentException e){
	    	logger.trace("Page:" + field.getDeclaringClass().getSimpleName() + " Field:" 
	    			+ field.getName() + " is not fit for WebElementLocator");
	    	return null;
	    }
	  }

}
